home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-02-20 | 16.2 KB | 374 lines | [TEXT/CWIE] |
- /*
- File: ComparisonOperand.cp
-
- Contains: xxx put contents here xxx
-
- Written by: Andy Nicholas, Greg Anderson, Chris Bingham, John Rohrlich, Max McFarland, Paul Ossenbruggen, Mike Kobb
-
- Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
-
- <4> 9/26/95 pco
-
- */
-
-
- #include "ComparisonOperand.h"
- #include "AbstractScriptableObject.h"
- #include "ResolveObjectSpecifier.h"
-
- //========================================================================================
- // CLASS TComparisonOperand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // TComparisonOperand::TComparisonOperand
- //----------------------------------------------------------------------------------------
- TComparisonOperand::TComparisonOperand()
- {
- } // TComparisonOperand::TComparisonOperand
-
- //----------------------------------------------------------------------------------------
- // TComparisonOperand::~TComparisonOperand
- //----------------------------------------------------------------------------------------
- TComparisonOperand::~TComparisonOperand()
- {
- } // TComparisonOperand::~TComparisonOperand
-
- //----------------------------------------------------------------------------------------
- // TComparisonOperand::DisposeOperandData
- //
- // Comparison operands that do not cache the data they return must dispose of said data
- // when it is passed back in to this routine (however, most comparison operands SHOULD
- // cache the data and dispose it only when the operand class is disposed).
- //----------------------------------------------------------------------------------------
- void TComparisonOperand::DisposeOperandData(TDescriptor /*operandData*/) const
- {
- } // TComparisonOperand::DisposeOperandData
-
- //----------------------------------------------------------------------------------------
- // TComparisonOperand::Compare
- //----------------------------------------------------------------------------------------
- Boolean TComparisonOperand::Compare(const TAETransaction& t, DescType comparisonOperator, const TComparisonOperand& compareWith, TAbstractScriptableObject* objectBeingExamined) const
- {
- DescType myBestType = this->BestType(t, objectBeingExamined);
- DescType otherBestType = compareWith.BestType(t, objectBeingExamined);
- DescType dataTypeToUse = myBestType;
-
- //
- // We will use the best data type for this comparison operand
- // UNLESS the operand we are comparing with cannot return that
- // data type and we can return its data type, in which case
- // we will use its best type instead.
- //
- if((compareWith.CanReturnDataOfType(t, myBestType, objectBeingExamined) == false) && (this->CanReturnDataOfType(t, otherBestType, objectBeingExamined) == true))
- dataTypeToUse = otherBestType;
-
- //
- // Get the data from the respective comparison operands and
- // do the compare
- //
- TDescriptor myOperandData = this->GetData(t, dataTypeToUse, objectBeingExamined);
- TDescriptor otherOperandData = compareWith.GetData(t, dataTypeToUse, objectBeingExamined);
-
- Boolean result = myOperandData.Compare(comparisonOperator, otherOperandData);
-
- //
- // Dispose the operand data if it is no longer needed.
- //
- this->DisposeOperandData(myOperandData);
- compareWith.DisposeOperandData(otherOperandData);
-
- return result;
- } // TComparisonOperand::Compare
-
- //----------------------------------------------------------------------------------------
- // TComparisonOperand::Compare
- //----------------------------------------------------------------------------------------
- Boolean TComparisonOperand::Compare(const TAETransaction& t, DescType comparisonOperator, const DescType dataTypeToUse, TDescriptor otherOperandData, TAbstractScriptableObject* objectBeingExamined) const
- {
- //
- // Get the data from the respective comparison operands and
- // do the compare
- //
- TDescriptor myOperandData = this->GetData(t, dataTypeToUse, objectBeingExamined);
- Boolean result = myOperandData.Compare(comparisonOperator, otherOperandData);
-
- //
- // Dispose the operand data if it is no longer needed.
- //
- this->DisposeOperandData(myOperandData);
-
- return result;
- } // TComparisonOperand::Compare
-
- //----------------------------------------------------------------------------------------
- // TComparisonOperand::ComparisonOperandType
- //----------------------------------------------------------------------------------------
- ComparitorType TComparisonOperand::ComparisonOperandType() const
- {
- return kAbstractComparitor;
- }
-
- //----------------------------------------------------------------------------------------
- // TComparisonOperand::PropertyToCompare
- //----------------------------------------------------------------------------------------
- DescType TComparisonOperand::PropertyToCompare() const
- {
- return typeNull;
- }
-
- //========================================================================================
- // CLASS TConstantComparisonOperand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // TConstantComparisonOperand::TConstantComparisonOperand
- //----------------------------------------------------------------------------------------
- TConstantComparisonOperand::TConstantComparisonOperand(TDescriptor constantData, Boolean passOwnershipOfData /* = true*/)
- {
- fConstantData = constantData;
- fDataOwned = passOwnershipOfData;
- }
-
- //----------------------------------------------------------------------------------------
- // TConstantComparisonOperand::~TConstantComparisonOperand
- //----------------------------------------------------------------------------------------
- TConstantComparisonOperand::~TConstantComparisonOperand()
- {
- if(fDataOwned)
- fConstantData.Dispose();
- }
-
- //----------------------------------------------------------------------------------------
- // TConstantComparisonOperand::BestType
- //----------------------------------------------------------------------------------------
- DescType TConstantComparisonOperand::BestType(const TAETransaction&, TAbstractScriptableObject* /*objectBeingExamined*/) const
- {
- return fConstantData.DescriptorType();
- }
-
- //----------------------------------------------------------------------------------------
- // TConstantComparisonOperand::CanReturnDataOfType
- //----------------------------------------------------------------------------------------
- Boolean TConstantComparisonOperand::CanReturnDataOfType(const TAETransaction&, DescType desiredType, TAbstractScriptableObject* /*objectBeingExamined*/) const
- {
- return fConstantData.DescriptorType() == desiredType;
- }
-
- //----------------------------------------------------------------------------------------
- // TConstantComparisonOperand::GetData
- //----------------------------------------------------------------------------------------
- TDescriptor TConstantComparisonOperand::GetData(const TAETransaction&, DescType /*requestedType*/, TAbstractScriptableObject* /*objectBeingExamined*/) const
- {
- return fConstantData;
- }
-
- //----------------------------------------------------------------------------------------
- // TConstantComparisonOperand::SpecifierForOperand
- //----------------------------------------------------------------------------------------
- TDescriptor TConstantComparisonOperand::SpecifierForOperand()
- {
- TDescriptor result;
-
- result = fConstantData.Clone();
-
- return result;
- } // TConstantComparisonOperand::SpecifierForOperand
-
- //----------------------------------------------------------------------------------------
- // TConstantComparisonOperand::ComparisonOperandType
- //----------------------------------------------------------------------------------------
- ComparitorType TConstantComparisonOperand::ComparisonOperandType() const
- {
- return kLiteralComparitor;
- }
-
- //========================================================================================
- // CLASS TPropertyComparisonOperand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::TPropertyComparisonOperand
- //----------------------------------------------------------------------------------------
- TPropertyComparisonOperand::TPropertyComparisonOperand(DescType propertyToCompare, TAbstractObjectSpecifier* relativeSpecifier, Boolean isRelativeToObjectBeingExamined) :
- fPropertyToCompare(propertyToCompare),
- fRelativeSpecifier(relativeSpecifier),
- fIsRelativeToObjectBeingExamined(isRelativeToObjectBeingExamined),
- fCachedType(typeNull),
- fObjectBeingExaminedForDataCache(nil),
- fCachedResolvedObject(nil),
- fObjectBeingExaminedForResolveCache(nil)
- {
- fCachedData.ClearDescriptor();
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::~TPropertyComparisonOperand
- //----------------------------------------------------------------------------------------
- TPropertyComparisonOperand::~TPropertyComparisonOperand()
- {
- delete fRelativeSpecifier;
- fCachedData.Dispose();
- if(fCachedResolvedObject != nil)
- fCachedResolvedObject->DisposeDesignator();
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::ResolveObjectBeingExamined
- //----------------------------------------------------------------------------------------
- TAbstractScriptableObject* TPropertyComparisonOperand::ResolveObjectBeingExamined(const TAETransaction& t, TAbstractScriptableObject* objectBeingExamined) const
- {
- //
- // If our relative specifier isn't relative after all, then
- // clear out the 'objectBeingExamined' variable, because it's
- // irrelevant to the results of the resolution, and we don't
- // want to force the cache to be cleared if it changes.
- //
- if(fIsRelativeToObjectBeingExamined == false)
- objectBeingExamined = nil;
- TAbstractScriptableObject* result = nil;
-
- //
- // If we haven't cached anything, or if the object that the
- // cached item is based on changes, then we need to resolve
- // the relative specifier to determine the object to access.
- //
- if((fCachedResolvedObject == nil) || (fObjectBeingExaminedForResolveCache != objectBeingExamined))
- {
- //
- // Clear the old cache, if any
- //
- if(fCachedResolvedObject != nil)
- ((TPropertyComparisonOperand*)this)->fCachedResolvedObject->DisposeDesignator();
- ((TPropertyComparisonOperand*)this)->fObjectBeingExaminedForResolveCache = objectBeingExamined;
-
- //
- // If we have a relative specifier, then resolve it. If
- // we don't have a relative specifier, then use the null
- // container if this property is not relative to the object
- // being examined. We never cache the object being examined,
- // though, because we don't own it, and we wouldn't want
- // to double-dispose when this object goes away.
- //
- if(fRelativeSpecifier != nil)
- ((TPropertyComparisonOperand*)this)->fCachedResolvedObject = fRelativeSpecifier->ResolveToken(t, objectBeingExamined);
- else if(fIsRelativeToObjectBeingExamined == false)
- ((TPropertyComparisonOperand*)this)->fCachedResolvedObject = GetNullContainer();
- result = fCachedResolvedObject;
- }
-
- //
- // If we are relative to the object being examined, and we
- // don't have a relative specifier, then use the object
- // being examined as the base for our comparison.
- //
- if(result == nil)
- result = objectBeingExamined;
-
- return result;
- } // TPropertyComparisonOperand::ResolveObjectBeingExamined
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::BestType
- //----------------------------------------------------------------------------------------
- DescType TPropertyComparisonOperand::BestType(const TAETransaction& t, TAbstractScriptableObject* objectBeingExamined) const
- {
- objectBeingExamined = this->ResolveObjectBeingExamined(t, objectBeingExamined);
-
- return objectBeingExamined ? objectBeingExamined->BestType(t, fPropertyToCompare) : typeNull;
- } // TPropertyComparisonOperand::BestType
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::CanReturnDataOfType
- //----------------------------------------------------------------------------------------
- Boolean TPropertyComparisonOperand::CanReturnDataOfType(const TAETransaction& t, DescType desiredType, TAbstractScriptableObject* objectBeingExamined) const
- {
- objectBeingExamined = this->ResolveObjectBeingExamined(t, objectBeingExamined);
-
- return objectBeingExamined ? objectBeingExamined->CanReturnDataOfType(t, fPropertyToCompare, desiredType) : false;
- } // TPropertyComparisonOperand::CanReturnDataOfType
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::GetData
- //----------------------------------------------------------------------------------------
- TDescriptor TPropertyComparisonOperand::GetData(const TAETransaction& t, DescType requestedType, TAbstractScriptableObject* objectBeingExamined) const
- {
- objectBeingExamined = this->ResolveObjectBeingExamined(t, objectBeingExamined);
- TDescriptor result;
-
- if(objectBeingExamined != nil)
- {
- if((fObjectBeingExaminedForDataCache != objectBeingExamined) || (fCachedType != requestedType))
- {
- ((TPropertyComparisonOperand*)this)->fCachedData.Dispose();
- ((TPropertyComparisonOperand*)this)->fCachedType = typeNull;
- ((TPropertyComparisonOperand*)this)->fObjectBeingExaminedForDataCache = nil;
- }
-
- result = objectBeingExamined->GetDataOfType(t, fPropertyToCompare, requestedType);
- ((TPropertyComparisonOperand*)this)->fCachedData = result;
- ((TPropertyComparisonOperand*)this)->fObjectBeingExaminedForDataCache = objectBeingExamined;
- ((TPropertyComparisonOperand*)this)->fCachedType = requestedType;
- }
-
- return result;
- } // TPropertyComparisonOperand::GetData
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::Compare
- //----------------------------------------------------------------------------------------
- Boolean TPropertyComparisonOperand::Compare(const TAETransaction& t, DescType comparisonOperator, const DescType /*dataTypeToUse*/, TDescriptor otherOperandData, TAbstractScriptableObject* objectBeingExamined) const
- {
- objectBeingExamined = this->ResolveObjectBeingExamined(t, objectBeingExamined);
-
- return objectBeingExamined->CompareProperty(t, fPropertyToCompare, comparisonOperator, otherOperandData);
- } // TPropertyComparisonOperand::Compare
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::SpecifierForOperand
- //----------------------------------------------------------------------------------------
- TDescriptor TPropertyComparisonOperand::SpecifierForOperand()
- {
- TDescriptor result;
- TDescriptor nullContainer;
-
- if(fIsRelativeToObjectBeingExamined == false)
- result.MakeObjectSpecifierForProperty(fPropertyToCompare, nullContainer, false);
- else if(fRelativeSpecifier == nil)
- result.MakeSpecifierForPropertyOfObjectBeingExamined(fPropertyToCompare);
- else
- {
- TDescriptor container = fRelativeSpecifier->BuildObjectSpecifier();
- result.MakeObjectSpecifierForProperty(fPropertyToCompare, container, true);
- }
-
- return result;
- } // TPropertyComparisonOperand::SpecifierForOperand
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::ComparisonOperandType
- //----------------------------------------------------------------------------------------
- ComparitorType TPropertyComparisonOperand::ComparisonOperandType() const
- {
- //
- // ◊Script: we should claim to be a kSimplePropertyComparitor if there is a
- // relative specifier that is nothing more than "of object being examined,"
- // and we should claim to be a kLiteralComparitor if the relative specifier
- // does NOT end with "of object being examined."
- //
- if(fRelativeSpecifier == nil)
- return kSimplePropertyComparitor;
- else
- return kCompoundPropertyComparitor;
- }
-
- //----------------------------------------------------------------------------------------
- // TPropertyComparisonOperand::PropertyToCompare
- //----------------------------------------------------------------------------------------
- DescType TPropertyComparisonOperand::PropertyToCompare() const
- {
- return fPropertyToCompare;
- }
-
-
-